package com.taptech.doufu.ui.activity;
import android.content.Intent;
import android.os.Bundle;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.base.DiaobaoBaseActivity;
import com.taptech.doufu.ui.adapter.CartoonEpisodeMenuAdapter;
import com.taptech.doufu.bean.cartoon.CartoonDetailGsonBean;
import com.taptech.doufu.bean.cartoon.WholeCartoonDetaisBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.ui.view.drawcircle.MyRecyclerView;
import com.taptech.doufu.event.EventBusCartoonCatlog;
import com.taptech.doufu.net.retrofit.api.ApiClient;
import com.taptech.doufu.net.retrofit.api.BaseSubscriber;
import com.taptech.doufu.net.retrofit.api.RxJavaHelper;
import com.taptech.doufu.util.sp.DiaoBaoSharedPreferences;
import com.taptech.doufu.ui.view.WaitDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by longsiyang on 15/12/11.
 */
public class CartoonEpisodeMenuActivity extends DiaobaoBaseActivity implements View.OnClickListener {

    private static final String TAG = "CartoonEpisodeMenuActiv";
    // Ui View
    MyRecyclerView gridView;
    CartoonEpisodeMenuAdapter adapter;
    // 顺序控制按钮
    LinearLayout mOrderLayout;
    TextView mOrderTextView;
    ImageView mOrderImg;

    WaitDialog mDialog;

    //Ui data
    private String cartoonId;

    // data
    int currentPos = -1;
    WholeCartoonDetaisBean bean;
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onRefrush(EventBusCartoonCatlog event){
        if (event!=null && event.getTag().equals(EventBusCartoonCatlog.tag)){
            getData();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        addShadeBg();
        setContentView(R.layout.activity_cartoon_episode_menu_layout);
        initData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void initData(){
        Intent intent = getIntent();
        bean = (WholeCartoonDetaisBean) intent.getSerializableExtra(Constant.DATA);
        if (bean == null){
            cartoonId = intent.getStringExtra(Constant.CARTOON_ID);
            currentPos = DiaoBaoSharedPreferences.getSharedPreferencesValueToInt(CartoonScanNewActivity.SP_KEY_CARTOON_READ_HISTORY+cartoonId,this,0);
            getData();
        }else {
            if (bean != null && bean.getDetail() != null) {
                cartoonId = bean.getDetail().getId();
                currentPos = DiaoBaoSharedPreferences.getSharedPreferencesValueToInt(CartoonScanNewActivity.SP_KEY_CARTOON_READ_HISTORY+cartoonId,this,0);
            }
            initView(bean);
        }

    }

    private void getData() {
        ApiClient.getInstance().setUseCache(false).getService().getCartoonAllChapters(cartoonId)
                .compose(RxJavaHelper.<CartoonDetailGsonBean>observeOnMainThread(this))
                .subscribe(new BaseSubscriber<CartoonDetailGsonBean>(){
                    @Override
                    public void onNext(CartoonDetailGsonBean gsonBean) {
                        super.onNext(gsonBean);
                        if (gsonBean != null && gsonBean.getStatus() == Constant.STATUS_SUCCESS
                                && gsonBean.getData() != null) {
                            bean = gsonBean.getData();
                            initView(bean);
                        }else{
                            Toast.makeText(CartoonEpisodeMenuActivity.this, "数据获取失败", Toast.LENGTH_LONG).show();
                        }
                        dismissDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        try {
                            dismissDialog();
                            Toast.makeText(CartoonEpisodeMenuActivity.this, "网络异常，请检查网络后再试", Toast.LENGTH_LONG).show();
                        } catch (Exception e1) {

                        }
                    }
                });
        initsDialog();
    }

    private void dismissDialog() {
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }

    private void initsDialog()
    {
        mDialog = new WaitDialog(this, R.style.loadNovelDialog, "");
        mDialog.setCanceledOnTouchOutside(false);
        mDialog.show();
    }

    private void initView(WholeCartoonDetaisBean bean){

        adapter = new CartoonEpisodeMenuAdapter(this,bean);
        gridView = (MyRecyclerView) findViewById(R.id.menu_gridview);
        RecyclerView.LayoutManager manager = new StaggeredGridLayoutManager( 1 , StaggeredGridLayoutManager.VERTICAL );
        gridView.setLayoutManager(manager);
        gridView.setAdapter(adapter);

        mOrderLayout = (LinearLayout) findViewById(R.id.choose_section_order);
        mOrderImg = (ImageView) findViewById(R.id.section_order_img);
        mOrderTextView = (TextView) findViewById(R.id.section_order_text);
        mOrderLayout.setOnClickListener(this);
        if (adapter.orderSec){
            mOrderTextView.setText("正序");
            mOrderImg.setImageResource(R.drawable.novel_section_up_icon);
        }else {
            mOrderTextView.setText("倒序");
            mOrderImg.setImageResource(R.drawable.novel_section_down_icon);
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.choose_section_order:
                if (adapter.orderSec){
                    mOrderTextView.setText("倒序");
                    mOrderImg.setImageResource(R.drawable.novel_section_down_icon);
                    adapter.orderSec = false;
                }else {
                    mOrderTextView.setText("正序");
                    mOrderImg.setImageResource(R.drawable.novel_section_up_icon);
                    adapter.orderSec = true;
                }
                adapter.notifyDataSetChanged();
                break;
        }
    }
}
